﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.IO;


namespace TranTekBE.KPI_NhanSu
{
    public partial class Them_Huy_Chuong_Pharmacity : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserInfo.AdminKPI == true)
            {
                ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
                if (!IsPostBack)
                {
                    GetDropListAchievements(droplist_Achievements);
                    //
                    string[] filePaths = Directory.GetFiles(Server.MapPath("~/images/KPI/Achievements/"));
                    //
                    string sql_Achievements = "SELECT ID_DROPLIST_EVENT, NAME_EVENT FROM tbl_DropListEvent";
                    DataSet Event_Achievement = FunctionsUtils.Database2.ExecuteQuery(sql_Achievements);
                    string sql_filename = "";
                    //
                    foreach (DataRow rows in Event_Achievement.Tables[0].Rows)
                    {
                        string fileimg = rows["ID_DROPLIST_EVENT"].ToString();
                        sql_filename = fileimg + ".png";
                        string path_image = Server.MapPath("~/images/KPI/Achievements/") + sql_filename;
                        if (System.IO.File.Exists(path_image))
                        {
                            ltlDivDynamic_img.Text += @"
                        <a onclick='alert(" + fileimg + @");'>
                        <img id='" + fileimg + @"' style='width:90px;margin-left:5px' src='../images/KPI/Achievements/" + sql_filename + @"' alt='" + rows["NAME_EVENT"].ToString() + @"' title='" + rows["NAME_EVENT"].ToString() + @"' />
                        </a>
                        ";
                        }
                    }

                }
            }
            else
            {
                Response.Redirect("../Admin/DontPermission.aspx");
            }
        }

        public static void GetDropListAchievements(DropDownList ddl)
        {
            DataSet ds = FunctionsUtils.Database2.ExecuteQuery("SELECT ID_DROPLIST_EVENT, NAME_EVENT FROM tbl_DropListEvent");
            FunctionsUtils.BindDataForControlList(ddl, ds, "ID_DROPLIST_EVENT", "NAME_EVENT", "", "Chọn Achievements");
        }
        protected void Upload(object sender, EventArgs e)
        {
            try
            {
                if (FileUpload_Image.HasFile)
                {
                    if (CheckFileType(FileUpload_Image.FileName))
                    {
                        string filename = droplist_Achievements.SelectedValue.ToString() + ".png";
                        string path_image = Server.MapPath("~/images/KPI/Achievements/") + filename;

                        //
                        if (System.IO.File.Exists(path_image))
                        {
                            System.IO.File.Delete(path_image);
                            FileUpload_Image.PostedFile.SaveAs(path_image);
                            Response.Redirect(Request.Url.AbsoluteUri);
                        }
                        else
                        {
                            FileUpload_Image.PostedFile.SaveAs(path_image);
                            Response.Redirect(Request.Url.AbsoluteUri);
                        }

                    }
                    else
                    {
                        ltAddErr.Text = "File upload không đúng!";
                    }
                }
            }
            catch (Exception err)
            {
                ltAddErr.Text = err.Message;
            }
        }
        bool CheckFileType(string fileName)
        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())
            {

                case ".gif":

                    return true;

                case ".png":

                    return true;

                case ".jpg":

                    return true;

                case ".jpeg":

                    return true;

                default:

                    return false;

            }

        }

        protected void Delete(object sender, EventArgs e)
        {
            Delete_achievements(droplist_Achievements.SelectedValue.ToString());
        }
        void Delete_achievements(string filename_del)
        {
            string filename = filename_del + ".png";
            string path_image = Server.MapPath("~/images/KPI/Achievements/") + filename;

            //
            if (System.IO.File.Exists(path_image))
            {
                System.IO.File.Delete(path_image);
                Response.Redirect(Request.Url.AbsoluteUri);
            }
        }
    }
}